--[[
   Copyright (c) 2012 TPV R&D Center Brazil
   All rights reserved

   FileName: gui.resources.templateTables

   Created by: Addison Lima
   Date: 09/11/2012
--]]
------------------------------------------------------------------------
--requires
local tPrefenceApp = require('zapper.app.preference')
local tChannelApp = require('zapper.app.channel')
local tGlobalSkins = require('zapper.gui.resources.skinTables').tSkin1
local tLanguageStrTables = require('zapper.gui.resources.stringTables')
------------------------------------------------------------------------
--local variables
local tGlobalStrings = {}
------------------------------------------------------------------------
--module table
--module description - This is a template of a String module
local tTemplateTables = {}
------------------------------------------------------------------------

--- Synchronous function to get left footer of channel list when there is no channel
-- @return panel toolkit component with EXIT button and label
function tTemplateTables.getListNoChannelLeftFooter()
   tGlobalStrings = tLanguageStrTables['t' .. tPrefenceApp.getCurrentLanguage() .. 'Strings']
   --left side
   local tkPnFooter = panel:new(tGlobalSkins.tFooter['tListLeft'])
   --exit
   local tkImButton = image:new(tGlobalSkins.tFooter['tButtonExit'])
   local tkLbLabel  = label:new(tGlobalSkins.tFooter['tLabelArea'])
   tkLbLabel:setFont(tGlobalSkins.tFont['t16Regular'])
   tkLbLabel:setText{tGlobalStrings.tCommon['sExit']}
   tkLbLabel:setShadow{}
   tkPnFooter:add{tkImButton, tkLbLabel}
   --clear
   tkImButton = nil
   tkLbLabel  = nil

   tkPnFooter:realign{direction = 'LEFT', spacing = 10}

   return tkPnFooter
end

--- Synchronous function to get right footer of channel list when there is no channel
-- @return panel toolkit component with SCAN CHANNELS button and label
function tTemplateTables.getListNoChannelRightFooter()
   tGlobalStrings = tLanguageStrTables['t' .. tPrefenceApp.getCurrentLanguage() .. 'Strings']
   --right side
   local tkPnFooter = panel:new(tGlobalSkins.tFooter['tListRight'])
   --scan channels
   local tkImButton = image:new(tGlobalSkins.tFooter['tButtonBlue'])
   local tkLbLabel  = label:new(tGlobalSkins.tFooter['tLabelArea'])
   tkLbLabel:setFont(tGlobalSkins.tFont['t16Regular'])
   tkLbLabel:setText{tGlobalStrings.tCommon['sScanChannel']}
   tkLbLabel:setShadow{}
   tkPnFooter:add{tkLbLabel, tkImButton}
   --clear
   tkImButton = nil
   tkLbLabel  = nil

   tkPnFooter:realign{direction = 'RIGHT', spacing = 10}

   return tkPnFooter
end

--- Synchronous function to get left footer of channel list
-- @return panel toolkit component with OK and EXIT buttons and labels
function tTemplateTables.getListLeftFooter()
   tGlobalStrings = tLanguageStrTables['t' .. tPrefenceApp.getCurrentLanguage() .. 'Strings']
   --left side
   local tkPnFooter = panel:new(tGlobalSkins.tFooter['tListLeft'])
   local tkPnSpace  = panel:new(tGlobalSkins.tFooter['tSpace'])
   --ok
   local tkImButton = image:new(tGlobalSkins.tFooter['tButtonOk'])
   local tkLbLabel  = label:new(tGlobalSkins.tFooter['tLabelArea'])
   tkLbLabel:setFont(tGlobalSkins.tFont['t16Regular'])
   tkLbLabel:setText{tGlobalStrings.tCommon['sSelect']}
   tkLbLabel:setShadow{}
   tkPnFooter:add{tkImButton, tkLbLabel, tkPnSpace}
   --exit
   tkImButton = image:new(tGlobalSkins.tFooter['tButtonExit'])
   tkLbLabel  = label:new(tGlobalSkins.tFooter['tLabelArea'])
   tkLbLabel:setFont(tGlobalSkins.tFont['t16Regular'])
   tkLbLabel:setText{tGlobalStrings.tCommon['sExit']}
   tkLbLabel:setShadow{}
   tkPnFooter:add{tkImButton, tkLbLabel}
   --clear
   tkPnSpace  = nil
   tkImButton = nil
   tkLbLabel  = nil

   tkPnFooter:realign{direction = 'LEFT', spacing = 10}

   return tkPnFooter
end

--- Synchronous function to get right footer of channel list
-- @return panel toolkit component with FAVORITES and EDIT buttons and labels
function tTemplateTables.getListRightFooter()
   tGlobalStrings = tLanguageStrTables['t' .. tPrefenceApp.getCurrentLanguage() .. 'Strings']
   --right side
   local tkPnFooter = panel:new(tGlobalSkins.tFooter['tListRight'])
   local tkPnSpace  = panel:new(tGlobalSkins.tFooter['tSpace'])
   --favorites
   local tkImButton = image:new(tGlobalSkins.tFooter['tButtonBlue'])
   local tkLbLabel  = label:new(tGlobalSkins.tFooter['tLabelArea'])
   tkLbLabel:setFont(tGlobalSkins.tFont['t16Regular'])
   tkLbLabel:setText{tGlobalStrings.tCommon['sFavorite']}
   tkLbLabel:setShadow{}
   tkPnFooter:add{tkLbLabel, tkImButton, tkPnSpace}
   --edit
--[[ TEMPORARY FOR DEMO
   tkImButton = image:new(tGlobalSkins.tFooter['tButtonYellow'])
   tkLbLabel  = label:new(tGlobalSkins.tFooter['tLabelArea'])
   tkLbLabel:setFont(tGlobalSkins.tFont['t16Regular'])
   tkLbLabel:setText{tGlobalStrings.tCommon['sEdit']}
   tkLbLabel:setShadow{}
   tkPnFooter:add{tkLbLabel, tkImButton}
--]]
   --clear
   tkPnSpace  = nil
   tkImButton = nil
   tkLbLabel  = nil

   tkPnFooter:realign{direction = 'RIGHT', spacing = 10}

   return tkPnFooter
end

--- Synchronous function to get message of no channels installed
-- @return label toolkit component with no channels installed message
function tTemplateTables.getListNoChannelMessage()
   tGlobalStrings = tLanguageStrTables['t' .. tPrefenceApp.getCurrentLanguage() .. 'Strings']
   local sCurTunerMode = tChannelApp.getCurrentTunerMode()
   local tkLbMessage = label:new(tGlobalSkins.tListChannel['tNoChannelMessage'])
   tkLbMessage:setShadow{}
   tkLbMessage:setText{tGlobalStrings.tChannelList['sNo' .. sCurTunerMode ..'Channel'], '', tGlobalStrings.tChannelList['sFindChannel']}

   return tkLbMessage
end

--- Synchronous function to get channel list
-- @return list toolkit component for channel list
function tTemplateTables.getWindowList(sScreen)
   local tkLbItem1
   local tkLbItem2
   local tkLtList = list:new(tGlobalSkins['tList'..sScreen]['tArea'])
   tkLtList:setPage(tGlobalSkins['tList'..sScreen]['tPage'])
   --load elements
   tkLbItem1 = label:new(tGlobalSkins['tList'..sScreen]['tItem1'])
   tkLbItem1:setShadow{}
   if tGlobalSkins['tList'..sScreen]['tItem2'] ~= nil then
      tkLbItem2 = label:new(tGlobalSkins['tList'..sScreen]['tItem2'])
      tkLbItem2:setShadow{}
      tkLtList:add{tkLbItem1,tkLbItem2}
   else
      tkLtList:add{tkLbItem1}
   end
   --load focus bar
   local tkImFocusBarLeft  = image:new(tGlobalSkins['tList'..sScreen]['tFocusBarLeft'])
   tkImFocusBarLeft:resize{height = 53}
   local tkImFocusBarRight = image:new(tGlobalSkins['tList'..sScreen]['tFocusBarRight'])
   tkImFocusBarRight:resize{height = 53}
   local tkImFocusBarMid   = image:new(tGlobalSkins['tList'..sScreen]['tFocusBarMid'])
   tkImFocusBarMid:resize(tGlobalSkins['tList'..sScreen]['tFocusBarMidSize'])
   --add elements into list
   tkLtList:addFocus{tkImFocusBarLeft, tkImFocusBarMid, tkImFocusBarRight}
   tkLtList:setData(tGlobalSkins['tList'..sScreen]['tData'])
   --clear
   tkLbItem1 = nil
   tkLbItem2 = nil
   tkImFocusBarLeft = nil
   tkImFocusBarMid = nil
   tkImFocusBarRight = nil

   return tkLtList
end

--- Synchronous function to get window screen header
-- @return table toolkit components with header label and background
function tTemplateTables.getWindowHeader(sScreen)
   tGlobalStrings = tLanguageStrTables['t' .. tPrefenceApp.getCurrentLanguage() .. 'Strings']
   --load title label
   local lTitle = label:new(tGlobalSkins['tList'..sScreen]['tTitleArea'])
   lTitle:setFont(tGlobalSkins['tList'..sScreen]['tTitleFont'])
   lTitle:setText{tGlobalStrings.tTitles['s'..sScreen..'List']}
   lTitle:setShadow(tGlobalSkins['tList'..sScreen]['tTitleShadow'])
   --load title background
   
   --resize == text_size - (2*((mid_init - left_init)-(text_init - left_init)))
   --text_size
   local nSizeX, nSizeY = lTitle:getTextSize()
   --mid_init - left_init
   local nGap = (tGlobalSkins['tList'..sScreen]['tTitleMid']['left']-tGlobalSkins['tList'..sScreen]['tTitleLeft']['left'])
   --((mid_init - left_init)-(text_init - left_init)))
   local nGap = nGap - (tGlobalSkins['tList'..sScreen]['tTitleArea']['left']-tGlobalSkins['tList'..sScreen]['tTitleLeft']['left'])
   --text_size - (2*((mid_init - left_init)-(text_init - left_init)))
   local nResize = nSizeX - (2*nGap)
   
   local nTitleRightBegin = tGlobalSkins['tList'..sScreen]['tTitleMid']['left'] + nResize
   
   local iTitleLeft  = image:new(tGlobalSkins['tList'..sScreen]['tTitleLeft'])
   local iTitleRight = image:new(tGlobalSkins['tList'..sScreen]['tTitleRight'])
   local iTitleMid   = image:new(tGlobalSkins['tList'..sScreen]['tTitleMid'])
   if nResize > 0 then 
      iTitleMid:resize{width = nResize}
      iTitleRight:setArea{ left = nTitleRightBegin }
   else
      iTitleMid:resize{width = 0}
      iTitleRight:setArea{ left = tGlobalSkins['tList'..sScreen]['tTitleMid']['left'] }
   end
   return iTitleLeft, iTitleMid, iTitleRight, lTitle
end

--- Synchronous function to get a window screen with border
-- @param sSource define the window (Channel, Source, ...)
-- @return panel toolkit component with window background and borders
function tTemplateTables.getWindowBackground(sSource)
   tArea = tGlobalSkins['tWindow'..sSource]['tListArea']
   local tTotalArea = {}
   --calculate total area left
   if(tArea.left - tGlobalSkins['tWindow']['nBorderSize'] > 0) then
      tTotalArea.left = tArea.left - tGlobalSkins['tWindow']['nBorderSize']
   else
      tTotalArea.left = 0
   end
   --calculate total area top
   if(tArea.top - tGlobalSkins['tWindow']['nBorderSize'] > 0) then
      tTotalArea.top = tArea.top - tGlobalSkins['tWindow']['nBorderSize']
   else
      tTotalArea.top = 0
   end
   --calculate total area width and height
   tTotalArea.width = tArea.width + (tGlobalSkins['tWindow']['nBorderSize'] * 2) --344+81+81 = 506
   tTotalArea.height = tArea.height + (tGlobalSkins['tWindow']['nBorderSize'] * 2) --477+81+81 = 639
   --create panel and background
   local tkPnBg = panel:new(tTotalArea)
   local tkPnBackground = panel:new(tArea)
   --calculate window left and top
   local nNewLeft = tArea.left - tTotalArea.left
   local nNewTop = tArea.top - tTotalArea.top
   --define window position
   tkPnBackground:setArea{left = nNewLeft, top = nNewTop}
   --load content gradient
   local tkImBorder = image:new(tGlobalSkins['tWindow']['tContentGradient'])
   tkImBorder:resize{width = tArea.width}
   --add background in panel
   tkPnBackground:add{tkImBorder}
   tkPnBg:add{tkPnBackground}
   --calculate left and top positions
   local n1stLeft = (tArea.left - tTotalArea.left) - tGlobalSkins['tWindow']['nBorderSize']
   local n2ndLeft = (tArea.left - tTotalArea.left) + tArea.width - tGlobalSkins['tWindow']['nBorderSize']
   local n1stTop = (tArea.top - tTotalArea.top) - tGlobalSkins['tWindow']['nBorderSize']
   local n2ndTop = (tArea.top - tTotalArea.top) + tArea.height - tGlobalSkins['tWindow']['nBorderSize']
   --load top-left corner
   tkImBorder = image:new(tGlobalSkins['tWindow']['tCornerTopLeft'])
   tkImBorder:setArea{left = n1stLeft, top = n1stTop}
   tkPnBg:add{tkImBorder}
   --load bottom-left corner
   tkImBorder = image:new(tGlobalSkins['tWindow']['tCornerBottomLeft'])
   tkImBorder:setArea{left = n1stLeft, top = n2ndTop}
   tkPnBg:add{tkImBorder}
   --load top-right corner
   tkImBorder = image:new(tGlobalSkins['tWindow']['tCornerTopRight'])
   tkImBorder:setArea{left = n2ndLeft, top = n1stTop}
   tkPnBg:add{tkImBorder}
   --load bottom-right corner
   tkImBorder = image:new(tGlobalSkins['tWindow']['tCornerBottomRight'])
   tkImBorder:setArea{left = n2ndLeft, top = n2ndTop}
   tkPnBg:add{tkImBorder}
   --calculate vertical and horizontal border width
   local nVBorderWidth = tArea.height - (tGlobalSkins['tWindow']['nBorderSize'] * 2)
   local nHBorderWidth = tArea.width - (tGlobalSkins['tWindow']['nBorderSize'] * 2)
   --readjust left and top positions
   n2ndLeft = (tArea.left - tTotalArea.left) + tGlobalSkins['tWindow']['nBorderSize']
   n2ndTop = (tArea.top - tTotalArea.top) + tGlobalSkins['tWindow']['nBorderSize']
   --load left line
   tkImBorder = image:new(tGlobalSkins['tWindow']['tLineLeft'])
   tkImBorder:setArea{left = n1stLeft, top = n2ndTop}
   tkImBorder:resize{width = tGlobalSkins['tWindow']['nBorderSize'], height = nVBorderWidth}
   tkPnBg:add{tkImBorder}
   --load top line
   tkImBorder = image:new(tGlobalSkins['tWindow']['tLineTop'])
   tkImBorder:setArea{left = n2ndLeft, top = n1stTop}
   tkImBorder:resize{width = nHBorderWidth, height = tGlobalSkins['tWindow']['nBorderSize']}
   tkPnBg:add{tkImBorder}
   --readjust left and top positions
   n1stLeft = (tArea.left - tTotalArea.left) + tArea.width
   n1stTop = (tArea.top - tTotalArea.top) + tArea.height
   --load right line
   tkImBorder = image:new(tGlobalSkins['tWindow']['tLineRight'])
   tkImBorder:setArea{left = n1stLeft, top = n2ndTop}
   tkImBorder:resize{width = tGlobalSkins['tWindow']['nBorderSize'], height = nVBorderWidth}
   tkPnBg:add{tkImBorder}
   --load bottom line
   tkImBorder = image:new(tGlobalSkins['tWindow']['tLineBottom'])
   tkImBorder:setArea{left = n2ndLeft, top = n1stTop}
   tkImBorder:resize{width = nHBorderWidth, height = tGlobalSkins['tWindow']['nBorderSize']}
   tkPnBg:add{tkImBorder}
   --clear
   tkImBorder = nil
   tkPnBackground = nil

   return tkPnBg
end

--- Synchronous function to translate channel name
-- @param channel number to translate
-- @return a string representing the channel number
function tTemplateTables.convertChannelName(nChanNo,sCurrLang)
   tGlobalStrings = tLanguageStrTables['t' .. sCurrLang .. 'Strings']
   local sChannelName = ' '
   local nH,nD,nU
   local nStrIdx = 0

   if(type(nChanNo) == "number") then
      nH = math.modf(nChanNo / 100)
      nD = math.mod((math.modf(nChanNo / 10)), 10)
      nU = math.mod(nChanNo , 10)

      if(nH == 1) then
         if(nD > 0 or nU > 0) then
            nStrIdx = #tGlobalStrings.tCommon.sAnalogChannelName
         else
            nStrIdx = #tGlobalStrings.tCommon.sAnalogChannelName - 1
         end
         sChannelName = string.format("%s",tGlobalStrings.tCommon.sAnalogChannelName[nStrIdx])
      elseif(nH > 1) then
         nH = 0
      end

      if(nD > 0 or nU >= 0) then
         if(nD == 1) then
            nStrIdx = ((nD * 10) + nU) + 2
            sChannelName = string.format("%s%s", sChannelName, tGlobalStrings.tCommon.sAnalogChannelName[nStrIdx])
         elseif(nD == 2 and sCurLang == 'Es') then
            nStrIdx =((nD * 10) + nU) + 2
            sChannelName = string.format("%s%s", sChannelName, tGlobalStrings.tCommon.sAnalogChannelName[nStrIdx])
         else
            if(sCurLang == 'Es') then
               nStrIdx = nD + 29
            else
               nStrIdx = nD + 20
            end

            if(nD > 0) then
               sChannelName = string.format("%s %s", sChannelName, tGlobalStrings.tCommon.sAnalogChannelName[nStrIdx])
               if(nU > 0) then
                  nStrIdx = nU + 2
                  sChannelName = string.format("%s%s%s", sChannelName, tGlobalStrings.tCommon.sAnalogChanNameSepStr, tGlobalStrings.tCommon.sAnalogChannelName[nStrIdx])
               end
            elseif(nD == 0) then
               nStrIdx = nU + 2
               sChannelName = string.format("%s %s", sChannelName, tGlobalStrings.tCommon.sAnalogChannelName[nStrIdx])
            end
         end
      end
   end

   return sChannelName
end

return tTemplateTables
